package com.example.bgImageSear.user.controller;

import com.example.bgImageSear.commons.Result;
import com.example.bgImageSear.user.entity.UserEntity;
import com.example.bgImageSear.user.service.IUserService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@RestController

//用户操作类
@RequestMapping("/user")
public class UserController {

    @Resource
    private IUserService userService;
    //获取用户信息
    @RequestMapping(value = "/getUser", method = RequestMethod.POST)
    public Result getUserByPassword(@RequestBody UserEntity userEntity) {
        UserEntity user = userService.getUserByPassword(userEntity);
        if (null == user) {
            return new Result(0, "没能获取到用户", null);
        }
        return new Result(1, "获取到用户", user);
    }

    //查询用户列表
    @RequestMapping("/list")
    public Result list() {
        List<UserEntity> userList = userService.getUserList();
        if (null == userList) {
            return new Result(0, "没能获取到用户", null);
        }
        return new Result(1, "获取到用户", userList);
    }

    //注册用户
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public Result save(@RequestBody UserEntity userEntity) {
        long num = userService.save(userEntity);
        if (0 == num) {
            return new Result(0, "注册失败", null);
        }
        return new Result(1, "注册成功", num);
    }

    //用户登录
    @PostMapping("/login")
    public Result login(HttpServletRequest request,
                        @RequestParam("username") String username,
                        @RequestParam("password") String password) {
        UserEntity userEntity = userService.userLogin(username, password);
        if (userEntity == null) {
            return new Result(0, "登录失败！", null);
        }
        HttpSession session = request.getSession();
        session.setAttribute("uid", userEntity.getId());
        return new Result(1, "登录成功！", userEntity);
    }

    //修改用户信息
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public Result update(@RequestBody UserEntity userEntity) {
        long num = userService.update(userEntity);
        if (0 == num) {
            return new Result(0, "修改用户失败", null);
        }
        return new Result(1, "修改用户成功", num);
    }


    //删除用户
    @RequestMapping(value = "/del", method = RequestMethod.POST)
    public Result del(@RequestBody Long[] ids) {
        long num = userService.del(ids);
        if (0 == num) {
            return new Result(0, "删除失败", null);
        }
        return new Result(1, "删除成功", num);
    }



    @GetMapping("/isLogin")
    public Result isLogin(HttpServletRequest request) {
        Object uid = request.getSession().getAttribute("uid");
        return new Result(1, "", uid != null);
    }

    @RequestMapping("/logOut")
    public void logOut(HttpServletRequest request) {
        HttpSession session = request.getSession();
        session.removeAttribute("uid");
    }
}
